import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/index'
import Classify from '../views/classify'
import Buying from '../views/buying'
import Shopping from '../views/shopping'
import User from '../views/user'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/classify',
    name: 'Classify',
    component: Classify
  },
  {
    path: '/buying',
    name: 'Buying',
    component: Buying
  },
  {
    path: '/shopping',
    name: 'Shopping',
    component: Shopping
  },
  {
    path: '/user',
    name: 'User',
    component: User
  },
  {
    path: '/login',
    name: 'Name',
    component: () => import('../views/login')
  },
  {
    path: '/details/:deta_id?',
    name: 'Details',
    component: () => import('../views/components/details')
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/components/search')
  },
  {
    path: '*',
    name: '404',
    component: () => import('../views/components/404')
  }
]

const router = new VueRouter({
  routes
})

// 路由拦截
router.beforeEach((to, from, next) => {
  if (to.path === '/user' || to.path === '/shopping') {
    if (localStorage.getItem('user')) {
      next()
    } else {
      router.push('/login')
    }
  } else {
    next()
  }
})

export default router
